package delegate4j;

import com.sun.istack.internal.Nullable;

import java.util.concurrent.FutureTask;

@FunctionalInterface
public interface IFunc2<T1, T2, TResult> extends IDelegate {

    TResult invoke(T1 t1, T2 t2) throws Exception;

    default FutureTask<TResult> invokeAsync(T1 t1, T2 t2, @Nullable AsyncCallback callback) {
        FutureTask<TResult> futureTask = new FutureTask<>(() -> {
            TResult result = invoke(t1, t2);
            if (callback != null) {
                callback.invoke(this, new Object[]{result});
            }
            return result;
        });

        ASYNC_DELEGATE_THREAD_POOL.submit(futureTask);
        return futureTask;
    }

}
